package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SubjectDataAdapter;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.entity.MaketSubject;
import com.supersit.greensproject.entity.PriceGather;
import com.supersit.greensproject.resources.Authority;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 远程同步
 * Created by cong on 2016/7/20.
 */
@ContentView(R.layout.activity_synchronization_menu)
public class SynchronizationMenuActivity extends BaseActivity {
    private Context context;
    private MyTitleBar myTitleBar;
    @ViewInject(R.id.lv_synchronization_menu_list)
    private ListView lvSynchroniationMenuList;
    private ArrayList<Map<Integer, String>> itemData;
    private SubjectDataAdapter adapter;
    private List<BaseBase> vegetablesList;//蔬菜
    private List<BaseBase> poultryList;//肉鸡
    private List<BaseBase> milchList;//奶牛
    private List<BaseBase> fishList;//渔业
    private List<BaseBase> pigList;//生猪
    private List<MaketSubject> maketList;//市场主体
    // private List<Mass> massDetectionList;//质量检测
    private List<PriceGather> priceList;//价格采集
    private DbManager db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        setListViewData();
        bindListener();

    }

    private void setTitleBar() {
        myTitleBar = new MyTitleBar(context, findViewById(R.id.synchronization_menu_title));
        myTitleBar.setTitleText(context.getResources().getString(R.string.synchronizationn));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
    }

    private void bindListener() {
        myTitleBar.setImg1Listener(onClickListener);
        lvSynchroniationMenuList.setOnItemClickListener(onItemClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
            }
        }
    };

    private void setListViewData() {
        db = XUtilsDbManager.getInstance();
        itemData = new ArrayList<Map<Integer, String>>();
        int[] maketAuthority = Authority.getMaketAuthority(context);
        int[] baseAuthority = Authority.getBaseAuthority(context);
        for (int i = 0; i < Resources.synchronizationMenu.length; i++) {

            if (i < 5) {
                for (int j = 0; j < baseAuthority.length; j++) {
                    if (i == baseAuthority[j]) {
                        Map<Integer, String> map = new HashMap<Integer, String>();
                        map.put(i, Resources.synchronizationMenu[i]);
                        itemData.add(map);
                    }
                }
            } else {
                if (0 == maketAuthority.length && i == 5 || 0 == maketAuthority.length && i == 7) {
                    continue;
                } else {
                    Map<Integer, String> map = new HashMap<Integer, String>();
                    map.put(i, Resources.synchronizationMenu[i]);
                    itemData.add(map);
                }
            }


        }
        adapter = new SubjectDataAdapter(context
                , itemData, Resources.HasMessige);
        lvSynchroniationMenuList.setAdapter(adapter);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int num = 0;
            Map<Integer, String> item = itemData.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            List<?> list = null;
            Intent intent = null;
            Bundle bundle = null;
            switch (num) {
                case 0:
                    try {
                        list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", 0).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchronizationBaseActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 0);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 1:
                    try {
                        list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", 1).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchronizationBaseActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 1);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 2:
                    try {
                        list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", 2).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchronizationBaseActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 2);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 3:
                    try {
                        list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", 3).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchronizationBaseActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 3);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 4:
                    try {
                        list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", 4).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchronizationBaseActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 4);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 5://市场主体
                    try {
                        list = db.selector(MaketSubject.class).where("isDelete", "=", 1).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchroizationMaketActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 5);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case 6://质量检测
                    //startActivity(new Intent(context, SynchronizationBaseActivity.class));
                    break;
                case 7://价格采集
                    try {
                        list = db.selector(PriceGather.class).where("isDelete", "=", 1).and("operationsTatus", ">=", 0).findAll();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    intent = new Intent(context, SynchroizationPriceGatherActivity.class);
                    bundle = new Bundle();
                    bundle.putSerializable("list", (Serializable) list);
                    bundle.putInt("baseType", 7);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                default:
                    break;


            }
        }
    };
}
